package com.uenpay.im.ui.old.activity.contacts;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.callback.OnCommonCallBack;
import com.uenpay.im.invokeitems.contacts.ReportFriendsInvokItem;
import com.uenpay.util.app.base.BaseActivity;

/**
 * 投诉
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class ReportContactsOrGroupActivity extends BaseActivity implements
        OnClickListener {

    private ImageView eroticism_iv, ad_iv, falseMessage_iv, harassMessage_iv,
            reactionary_msg_iv, others_iv;
    private boolean is_eroticism_selected, is_ad_selected, is_swindle_selected,
            is_tort_selected, is_others_selected;
    private RelativeLayout eroticism_layout, ad_layout, falseMessage_layout,
            harassMessage_layout, reactionary_message_layout, others_layout;
    private EditText editText;
    private View loading;
    private String to_cust_id = "";
    private int type = 1;
    private int spam_type_id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_report_contacts_layout;
    }

    @Override
    public void initView(View view) {
        to_cust_id = getIntent().getStringExtra("to_cust_id");
        type = getIntent().getIntExtra("type", 1);
        initView();
        initListener();
    }

    protected void initView() {

        loading = findViewById(R.id.loading);
        eroticism_iv = (ImageView) findViewById(R.id.eroticism_iv);
        ad_iv = (ImageView) findViewById(R.id.ad_iv);
        harassMessage_iv = (ImageView) findViewById(R.id.harass_message_iv);
        reactionary_msg_iv = (ImageView) findViewById(R.id.reactionary_msg_iv);
        falseMessage_iv = (ImageView) findViewById(R.id.false_message_iv);
        others_iv = (ImageView) findViewById(R.id.others_iv);
        eroticism_layout = (RelativeLayout) findViewById(R.id.eroticism_layout);
        ad_layout = (RelativeLayout) findViewById(R.id.ad_layout);
        falseMessage_layout = (RelativeLayout) findViewById(R.id.false_message_layout);
        harassMessage_layout = (RelativeLayout) findViewById(R.id.harass_message_layout);
        reactionary_message_layout = (RelativeLayout) findViewById(R.id.reactionary_message_layout);
        others_layout = (RelativeLayout) findViewById(R.id.others_layout);
        editText = (EditText) findViewById(R.id.editText);

        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.submit));
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.complaints_friend));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Utils.hideSoftKeyboard(editText);
                        ReportContactsOrGroupActivity.this.finish();
                    }
                });
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        String content = editText.getText().toString();
                        if (!TextUtils.isEmpty(content)) {
                            reportContact(content, to_cust_id);
                        } else {
                            IMApplication.showToast(getResources().getString(
                                    R.string.input_report_content));
                        }
                    }
                });
        eroticism_layout.setOnClickListener(this);
        ad_layout.setOnClickListener(this);
        harassMessage_layout.setOnClickListener(this);
        falseMessage_layout.setOnClickListener(this);
        others_layout.setOnClickListener(this);
        reactionary_message_layout.setOnClickListener(this);
    }

    private void reportContact(String content, String to_cust_id) {
        if (spam_type_id == 0) {
            IMApplication.showToast(getResources().getString(
                    R.string.choose_report_type));
            return;
        }
        loading.setVisibility(View.VISIBLE);
        ContactsHandle.reportFriends(content, to_cust_id, spam_type_id, type,
                new OnCommonCallBack() {

                    @Override
                    public void onSuccess(Object result) {
                        myHandler.sendEmptyMessage(0);
                        ReportFriendsInvokItem.ReportFriendsInvokItemResult results = (ReportFriendsInvokItem.ReportFriendsInvokItemResult) result;
                        if (result != null
                                && results.status == HttpInvokeResultCode.RESULT_OK) {
                            IMApplication.showToast(results.msg);
                            Utils.hideSoftKeyboard(editText);
                            ReportContactsOrGroupActivity.this.finish();
                        }
                    }

                    @Override
                    public void onFailed(String reason) {
                        myHandler.sendEmptyMessage(0);
                    }
                });
    }

    private Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 0) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            }

            return false;
        }
    });

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.title_right_layout) {
            String content = editText.getText().toString();
            if (!is_eroticism_selected && !is_ad_selected
                    && !is_swindle_selected && !is_tort_selected
                    && !is_others_selected) {
                IMApplication.showToast(getResources().getString(
                        R.string.the_type_not_null));
                return;
            }
            if (TextUtils.isEmpty(content)) {
                IMApplication.showToast(getResources().getString(
                        R.string.the_content_not_null));
                return;
            }

            updateSelectedType(1);

        } else if (i == R.id.reactionary_message_layout) {
            updateSelectedType(1);

        } else if (i == R.id.harass_message_layout) {
            updateSelectedType(2);

        } else if (i == R.id.eroticism_layout) {
            updateSelectedType(3);

        } else if (i == R.id.false_message_layout) {
            updateSelectedType(4);

        } else if (i == R.id.ad_layout) {
            updateSelectedType(5);

        } else if (i == R.id.others_layout) {
            updateSelectedType(6);

        }

    }

    private void updateSelectedType(int index) {
        spam_type_id = index;
        reactionary_msg_iv.setVisibility(View.GONE);
        harassMessage_iv.setVisibility(View.GONE);
        eroticism_iv.setVisibility(View.GONE);
        falseMessage_iv.setVisibility(View.GONE);
        ad_iv.setVisibility(View.GONE);
        others_iv.setVisibility(View.GONE);
        if (index == 1) {
            reactionary_msg_iv.setVisibility(View.VISIBLE);
        } else if (index == 2) {
            harassMessage_iv.setVisibility(View.VISIBLE);
        } else if (index == 3) {
            eroticism_iv.setVisibility(View.VISIBLE);
        } else if (index == 4) {
            falseMessage_iv.setVisibility(View.VISIBLE);
        } else if (index == 5) {
            ad_iv.setVisibility(View.VISIBLE);
        } else if (index == 6) {
            others_iv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackPressed() {
        Utils.hideSoftKeyboard(editText);
        finish();
        super.onBackPressed();
    }

}
